package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaizeli.website.mapper.EmployeeAcceptanceMapper;
import com.kaizeli.website.pojo.entity.EmployeeAcceptance;
import com.kaizeli.website.pojo.dto.EmployeeAcceptanceDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.EmployeeAcceptanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 人员验收服务实现类
 */
@Service
public class EmployeeAcceptanceServiceImpl extends ServiceImpl<EmployeeAcceptanceMapper, EmployeeAcceptance> implements EmployeeAcceptanceService {

    @Autowired
    private EmployeeAcceptanceMapper employeeAcceptanceMapper;

    @Override
    public Result<List<EmployeeAcceptanceDTO>> getEmployeeAcceptancesByAcceptanceId(Long acceptanceId) {
        try {
            List<EmployeeAcceptanceDTO> employeeAcceptances = employeeAcceptanceMapper.selectByAcceptanceId(acceptanceId);
            return Result.success(employeeAcceptances);
        } catch (Exception e) {
            return Result.error(500, "查询人员验收记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<EmployeeAcceptanceDTO>> getEmployeeAcceptancesByProjectId(Long projectId) {
        try {
            List<EmployeeAcceptanceDTO> employeeAcceptances = employeeAcceptanceMapper.selectByProjectId(projectId);
            return Result.success(employeeAcceptances);
        } catch (Exception e) {
            return Result.error(500, "查询人员验收记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<EmployeeAcceptanceDTO>> getEmployeeAcceptancesByEmployeeId(Long employeeId) {
        try {
            List<EmployeeAcceptanceDTO> employeeAcceptances = employeeAcceptanceMapper.selectByEmployeeId(employeeId);
            return Result.success(employeeAcceptances);
        } catch (Exception e) {
            return Result.error(500, "查询人员验收记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<EmployeeAcceptance> createEmployeeAcceptance(EmployeeAcceptance employeeAcceptance) {
        try {
            // 计算人工费用
            if (employeeAcceptance.getWorkHours() != null && employeeAcceptance.getHourlyRate() != null) {
                BigDecimal laborCost = employeeAcceptance.getWorkHours().multiply(employeeAcceptance.getHourlyRate());
                employeeAcceptance.setLaborCost(laborCost);
            }
            
            employeeAcceptance.setCreateTime(LocalDateTime.now());
            employeeAcceptance.setUpdateTime(LocalDateTime.now());
            save(employeeAcceptance);
            return Result.success(employeeAcceptance);
        } catch (Exception e) {
            return Result.error(500, "创建人员验收记录失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<EmployeeAcceptance> updateEmployeeAcceptance(EmployeeAcceptance employeeAcceptance) {
        try {
            // 重新计算人工费用
            if (employeeAcceptance.getWorkHours() != null && employeeAcceptance.getHourlyRate() != null) {
                BigDecimal laborCost = employeeAcceptance.getWorkHours().multiply(employeeAcceptance.getHourlyRate());
                employeeAcceptance.setLaborCost(laborCost);
            }
            
            employeeAcceptance.setUpdateTime(LocalDateTime.now());
            updateById(employeeAcceptance);
            return Result.success(employeeAcceptance);
        } catch (Exception e) {
            return Result.error(500, "更新人员验收记录失败：" + e.getMessage());
        }
    }

    @Override
    public Result<BigDecimal> calculateTotalWorkHours(Long acceptanceId) {
        try {
            BigDecimal totalWorkHours = employeeAcceptanceMapper.calculateTotalWorkHours(acceptanceId);
            return Result.success(totalWorkHours);
        } catch (Exception e) {
            return Result.error(500, "计算总工时失败：" + e.getMessage());
        }
    }

    @Override
    public Result<BigDecimal> calculateTotalLaborCost(Long acceptanceId) {
        try {
            BigDecimal totalLaborCost = employeeAcceptanceMapper.calculateTotalLaborCost(acceptanceId);
            return Result.success(totalLaborCost);
        } catch (Exception e) {
            return Result.error(500, "计算总人工费用失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<Void> batchCreateEmployeeAcceptances(List<EmployeeAcceptance> employeeAcceptances) {
        try {
            for (EmployeeAcceptance employeeAcceptance : employeeAcceptances) {
                // 计算人工费用
                if (employeeAcceptance.getWorkHours() != null && employeeAcceptance.getHourlyRate() != null) {
                    BigDecimal laborCost = employeeAcceptance.getWorkHours().multiply(employeeAcceptance.getHourlyRate());
                    employeeAcceptance.setLaborCost(laborCost);
                }
                
                employeeAcceptance.setCreateTime(LocalDateTime.now());
                employeeAcceptance.setUpdateTime(LocalDateTime.now());
            }
            
            saveBatch(employeeAcceptances);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "批量创建人员验收记录失败：" + e.getMessage());
        }
    }
}
